#       词频统计
# •    已经有一个瓦尔登湖的文本，“Walden.txt”
# •    统计这篇文章的词频（每个词出现的次数）
# •    不统计标点符号
# •    The和the被统计成同一个词（不区分大小写）
# •    输出格式：{ 'the': 10, 'is': 45....}

# 获取词频
import re
def get():
    global new_str
    new_str = 0
    list = []
    list1 =[]
    with open('Walden.txt','r',encoding="utf-8") as f:
        world = f.read()
        list = world.lower()
        new_str = re.sub(r'[\d().!",;?]', ' ', world)

        # from collections import Counter


get()

#统计每个单词
def stat():
    list1 = []
    dict1 = {}
    # one = 0
    # words_dict = dict(
    #单词存放到列表里
    for i in new_str:
        if i == '' or i == "--":
            pass
        else:
            list1.append(i)
            #计算单词出现次数，存入字典
        for i in list1:
            cout_word =list1.count(i)
            dict = {i:cout_word}
            dict1.update(dict)
    # return sort_word
    print(dict1)
stat()




